package servlet;

import bean.Admin;
import bean.User;
import service.AdminService;
import service.UserService;
import service.impl.AdminServiceImpl;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/login")
public class AloginServlet extends HttpServlet {
    private AdminService adminService = new AdminServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String admin_name = request.getParameter("admin_name");
        String password = request.getParameter("password");

        Admin admin = adminService.login(admin_name, password);
        if (admin != null) {
            request.getSession().setAttribute("admin",admin);
            request.setAttribute("message","登录成功");
            request.setAttribute("page","aindex.jsp");
            request.getRequestDispatcher("alert.jsp").forward(request,response);
        } else {
            request.setAttribute("message","登录失败");
            request.setAttribute("page","alogin.jsp");
            request.getRequestDispatcher("alert.jsp").forward(request,response);
        }
    }
}
